import { STORAGE_PREFIX, ADD_DATA_STORE } from '@/config';
import type { AddData } from '../type';
import { isArray } from '@/utils';

export default defineStore(
    'addDataStore',
    () => {
        // 所有的数据
        const addData = reactive<AddData>({
            workingHoursAddData: undefined, // 工作时段相关
            classSystemAddData: undefined, // 班制相关
            supplerAddData: undefined, // 供应商相关
            workshopAddData: undefined, // 车间相关
            procedureAddData: undefined, // 工序相关
            equipmentAddData: undefined, // 设备相关
            orderAddData: undefined, // 需求订单相关
            auxiliaryResourcesAddData: undefined, // 辅助资源相关
            materialsManagementAddData: undefined, // 物料管理相关
            memberAddData: undefined, // 成员管理相关
            roleManagementAddData: undefined, // 角色管理相关
            customerAddData: undefined, // 客户信息相关
            bomAddData: undefined, // bom
            processAddData: undefined // 工艺路线
        });

        /**
         * @: 处理数据
         * @param {要添加的对象key} key
         * @param {要添加的对象值} payload
         */
        const handleAddData = (key: keyof AddData, payload?: any) => {
            const data = isArray(payload) ? { tableData: payload } : payload;
            addData[key] = { update: true, ...data };
        };

        return {
            handleAddData,
            ...toRefs(addData)
        };
    },
    {
        persist: {
            key: `${STORAGE_PREFIX}${ADD_DATA_STORE}`
        }
    }
);
